﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Procure.Payment;
using System.Reflection;
using umbraco.presentation.nodeFactory;

namespace Procure.Web
{
    public class ProcureConfiguration
    {
        public static List<BaseGateway<IGatewaySettings>> Gateways { get; private set; }
        public static Node Checkout { get; private set; }
        public static Node AddToCart { get; private set; }

        public ProcureConfiguration()
        {
            XmlDocument xdoc = GetXmlDoc();
            LoadPaymentGateways(xdoc);
            LoadPages(xdoc);
        }

        #region Pages
        #region LoadPage
        private void LoadPages(XmlDocument xdoc)
        {
            Checkout = GetUmbracoNode(xdoc, "checkout");
            AddToCart = GetUmbracoNode(xdoc, "addtocart");
        }
        #endregion
        #region LoadPage
        private Node GetUmbracoNode(XmlDocument xdoc, string PageName)
        {
            XmlNode node = xdoc.SelectSingleNode(string.Format("//procure/pages/page [@key='{0}']", PageName));
            Node returnNode = null;
            if (node != null)
            {
                string nodeIdString = node.Attributes["value"].Value;
                int nodeId = 0;
                if (int.TryParse(nodeIdString, out nodeId))
                {
                    returnNode = new Node(nodeId);
                }
            }
            return returnNode;
        }
        #endregion
        #endregion

        #region PaymentGateways
        #region LoadPaymentGateways
        private void LoadPaymentGateways(XmlDocument xdoc)
        {
            Gateways = new List<BaseGateway<IGatewaySettings>>();

            XmlNodeList gateways = xdoc.SelectNodes("//procure/paymentGateways/paymentGateway");
            foreach (XmlNode gateway in gateways)
            {
                LoadPaymentGateway(gateway);
            }
        }
        #endregion
        #region LoadPaymentGateway
        private void LoadPaymentGateway(XmlNode gateway)
        {
            Dictionary<string, string> config = new Dictionary<string, string>();

            string assemblyName = gateway.Attributes["assembly"].Value;
            string typeName = gateway.Attributes["type"].Value;

            XmlNodeList configItems = gateway.SelectNodes("./config");
            foreach (XmlNode configItem in configItems)
                config.Add(configItem.Attributes["key"].Value, configItem.Attributes["value"].Value);

            Assembly assembly = Assembly.LoadFrom(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../bin/" + assemblyName + ".dll"));
            Type type = assembly.GetType(typeName);

            BaseGateway<IGatewaySettings> paymentGateway =
                Activator.CreateInstance(type, new object[] { config }) as BaseGateway<IGatewaySettings>;

            Gateways.Add(paymentGateway);
        }
        #endregion
        #endregion

        #region GetXmlDoc
        private static XmlDocument GetXmlDoc()
        {
            // Load task settings
            XmlDocument createDef = new XmlDocument();
            XmlTextReader defReader = new XmlTextReader(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/config/procure.xml"));
            createDef.Load(defReader);
            defReader.Close();
            return createDef;
        }
        #endregion
    }

    
}